package com.explorati.interview.interview.chapterten;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class SendThread extends Thread {

	private DatagramSocket ds;
	private int sendPort;

	public SendThread(int port, int sendPort) {
		super();
		this.sendPort = sendPort;
		try {
			this.ds = new DatagramSocket(port);
		} catch (SocketException e) {
			e.printStackTrace();
		}
	}

	public void run() {

		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String str = null;
			// 循环接收用户输入
			while ((str = br.readLine()) != null) {
				DatagramPacket dp;
				dp = new DatagramPacket(str.getBytes(), 0, str.length(), InetAddress.getByName("localhost"), sendPort);
				ds.send(dp);
				System.out.println("send : " + str);
			}

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			ds.close();
		}

	}
}
